<?php
include_once 'parsers/parser.php';
include_once 'classes/News.php';
include_once 'functions/calculateWeight.php';
include_once 'functions/NewsSort.php';
include_once 'functions/printNews.php';
include_once 'functions/searchString.php';

function getTrendingTopics(){
	
	$titlesKeyword = createTitlesKeyword(getTitlesArray());
	$todayTopics =  array();
	$property = 'properties/rssFeeds.xml';
	$arrayRss = simplexml_load_file($property);
	$i = 0;
	
	foreach($arrayRss as $rss) {
		$news = parse($rss->rssLocal, $rss->name, $rss->shortName );
	
		foreach($news as $eachNews){
			$title = $eachNews->getTitle();
			$resume = $eachNews->getResume();
			$weight = calculateWeight($titlesKeyword,$title,$resume);
			
			if ($weight > 0){
				$eachNews->setWeight($weight);
				$todayTopics[$i] = $eachNews;
				$i++;
			}
 		}
 	}
	$method = "peso";
	$todayTopicsSorted = NewsSort($todayTopics, $method);
	$topFive = array();
	$count = 0;
	
	while($count < 5){
		$topFive[$count] = $todayTopicsSorted[$count];
		$count++;
	}
// 	printAllNewsFromSource($topFive);
	printTrendingNews($topFive);
}

// devuelve un array con un titulo por posicion
function getTitlesArray(){

	$property = 'properties/rssFeeds.xml';
	$sourcesArray = simplexml_load_file($property);
	foreach($sourcesArray as $eachTitles) {
		$eachTitles = parse($eachTitles->rssLocal, $eachTitles->name, $eachTitles->shortName);
		$unwantedWordsArray = array("en","la","las","los","le","un","que","su","por","a","el","de","ellos","no","con","para","por","segun","mas","entre" );
		$eachTitles = str_replace($unwantedWordsArray, "", $eachTitles);
	}
	$titlesArray = array();
	$count = 0;
	foreach ($eachTitles as $foundTitle) {
		$titlesArray[$count] = $foundTitle->getTitle();
		$count++;
	}
	return $titlesArray;
}
// imprime un vector por pantalla. formato: una linea por posicion con el nro de posicion
function test($vectorTest){
	$count = 0;
	foreach ($vectorTest as $title) {
		echo '<p>La posicion ' . $count . ' es : ' . $title . '</p>';
		$count++;
	}
}

function createTitlesKeyword($stringArray){
	
	$formatArray = array();
	$tmpArray = array();
	$words = "";

	foreach ($stringArray as $term) {
		$words = $words . " " . $term;
	}
	$tmpArray = explode(" ",$words);
	$i = 0;

	foreach ($tmpArray as $topic) {
			$formatArray[$i] = $topic;
			$i++;
	}
	return $formatArray;
}

// solo para testear
 //getTrendingTopics();
?>